<template>
	<xn-form-container
		:title="formData.id ? '编辑坐班规则' : '增加坐班规则'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="名称：" name="sitName">
				<a-input v-model:value="formData.sitName" placeholder="请输入名称" allow-clear />
			</a-form-item>
			<a-form-item label="适用范围：" name="useRange">
				<a-tree-select
					v-model:value="formData.useRange"
					style="width: 100%"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择组织"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					multiple
					:tree-default-expanded-keys="treeDefaultExpandedKeys"
					:field-names="{
						children: 'children',
						label: 'name',
						value: 'id'
					}"
				/>
			</a-form-item>
			<a-form-item label="工作日：" name="workDay">
				<a-select v-model:value="formData.workDay" mode="multiple" placeholder="在职状态">
					<a-select-option v-for="item in workDay" :key="item.value" :value="item.value">{{
						item.label
					}}</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="班次：" name="shiftId">
				<a-select v-model:value="formData.shiftId" placeholder="班次" allow-clear>
					<a-select-option v-for="item in shiftDicts" :key="item.value" :value="item.value">{{
						item.label
					}}</a-select-option>
				</a-select>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="sysSitydutyRuleForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import sysSitydutyRuleApi from '@/api/biz/sysSitydutyRuleApi'
	import sysShiftManageApi from '@/api/biz/sysShiftManageApi'
	import userApi from '@/api/sys/userApi'
	import tool from '@/utils/tool'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const treeDefaultExpandedKeys = ref([])
	const treeData = ref([])
	const workDay = tool.dictList('CUSTOM_WORK_DAY')
	const shiftDicts = ref([])
	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			formData.value.workDay = JSON.parse(formData.value.workDay)
			formData.value.useRange = formData.value.useRange.split(',')
		}
		nextTick(() => {
			// 机构选择器数据
			userApi.userOrgTreeSelector().then((res) => {
				if (res !== null) {
					treeData.value = res
					// 默认展开2级
					treeData.value.forEach((item) => {
						// 因为0的顶级
						if (item.parentId === '0') {
							treeDefaultExpandedKeys.value.push(item.id)
							// 取到下级ID
							if (item.children) {
								item.children.forEach((items) => {
									treeDefaultExpandedKeys.value.push(items.id)
								})
							}
						}
					})
				}
			})
		})
		sysShiftManageApi.shiftManageDict().then((res) => {
			shiftDicts.value = res
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		sitName: [required('请输入名称')],
		useRange: [required('请输入适用范围')],
		workDay: [required('请输入工作日')],
		shiftId: [required('请输入班次')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.workDay = JSON.stringify(formDataParam.workDay)
			formDataParam.useRange = formDataParam.useRange.join(',')
			sysSitydutyRuleApi
				.sysSitydutyRuleSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
